package com.test.sdk.core.filter;

import com.test.sdk.common.pojo.CP;
import com.test.sdk.common.util.*;
import com.test.sdk.core.cache.CPCache;
import com.test.sdk.core.exception.SdkException;
import com.test.sdk.core.exception.SdkExceptionHandler;
import com.test.sdk.core.util.SdkConstants;
import com.test.sdk.core.util.TicketUtil;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @author 徒有琴
 */
@WebFilter("/*")
public class SystemFilter implements Filter {

    private final String NEED_LOGIN = "/user/service/.*";

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Integer cpId = RequestUtil.getInteger(request, "cpid");
        Integer seqNum = RequestUtil.getInteger(request, "seqnum");
        Integer prtChid = RequestUtil.getInteger(request, "prtchid");
        if (cpId == null || seqNum == null || prtChid == null) {
            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.PARAM_ERROR));
            return;
        }
        Map<String, String[]> params = request.getParameterMap();
        String signStr = SignUtil.getSignStr(params);
        System.out.println(signStr);
        try {
            CP cp = CPCache.getCpById(cpId);
            String mySign = SignUtil.md5(signStr + cp.getSecretKey());
            System.out.println(mySign);
            String sign = request.getParameter("sign");
            if (mySign.equals(sign)) {
                try {
                    String path = request.getServletPath();
                    if (path.matches(NEED_LOGIN)) {//必须登陆
                        String ticket = request.getParameter("ticket");
                        if (StringUtils.isEmpty(ticket)) {//登陆过期，没有票据
                            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.SESSION_TIME_OUT));
                            return;
                        }
                        Date time = TicketUtil.decodeDate(ticket);
                        if (time == null || (System.currentTimeMillis() - time.getTime()) > SdkConstants.LOGIN_TIMEOUT) {
                            SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.SESSION_TIME_OUT));
                            return;
                        }
                    }
                    filterChain.doFilter(request, response);
                } catch (Exception e) {
                    SdkExceptionHandler.handlerException(request, response, e);
                }
            } else {
                SdkExceptionHandler.handlerException(request, response, new SdkException(ErrorConstants.SIGN_ERROR));
            }
        } catch (SdkException e) {
            e.printStackTrace();
            SdkExceptionHandler.handlerException(request, response, e);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}
